package edu.xlh.travel.service.impl;

import edu.xlh.travel.common.Result;
import edu.xlh.travel.service.WeatherService;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

@Service
public class WeatherServiceImpl implements WeatherService  {
    @Override
    public Result getWeather() throws Exception{
        Result eResult = new Result();
        String weahter;

            URL url = new URL("http://www.weather.com.cn/data/sk/101240701.html");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.connect();
            InputStream inputStream = connection.getInputStream();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            int len = 0;
            byte[] buffer = new byte[1024];

            while ((len = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, len);
            }
            inputStream.close();
            byteArrayOutputStream.close();
            byte[] result = byteArrayOutputStream.toByteArray();
            weahter = new String(result, "utf-8");

        return new   Result(200,weahter);
    }
}
